package com.sunseen.intelligencebus

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.sunseen.intelligencebus.databinding.FragmentMainBinding
import com.sunseen.intelligencebus.home.HomeFragment
import com.sunseen.intelligencebus.riding.RidingFragment
import com.sunseen.intelligencebus.user.UserFragment
import me.yokeyword.fragmentation.ISupportFragment
import com.sunseen.intelligencebus.base.AbsBindingFragment


class MainFragment : AbsBindingFragment() {

    companion object {
        fun newInstance():MainFragment{
            return MainFragment()
        }
    }

    private val fragments: Array<ISupportFragment> =
        arrayOf(HomeFragment.newInstance(), RidingFragment.newInstance(), UserFragment.newInstance())

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->

        when (item.itemId) {
            R.id.navigation_home -> {
                showHideFragment(fragments[0])
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_control -> {
                showHideFragment(fragments[1])
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_user -> {
                showHideFragment(fragments[2])
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun setLayout(): Int {
        return R.layout.fragment_main
    }

    override fun onBindView(viewDataBinding: ViewDataBinding,bundle: Bundle?) {
        val binding = viewDataBinding as FragmentMainBinding
        binding.navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        loadMultipleRootFragment(R.id.fragment_container, 0, fragments[0], fragments[1], fragments[2])
    }
}